package com.songcha.module_mine.ui.activity.setting

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager.OnBackStackChangedListener
import com.alibaba.android.arouter.launcher.ARouter
import com.songcha.library_base.mvvm.base.BaseMvvmActivity
import com.songcha.library_business.config.FunctionConfig
import com.songcha.library_business.provider.IClearToMainActivityProvider
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.ui.view.HeaderBarView
import com.songcha.library_common.util.ToastUtil
import com.songcha.module_mine.R
import com.songcha.module_mine.databinding.MineActivitySettingBinding
import com.songcha.module_mine.ui.activity.MainActivity
import com.songcha.module_mine.ui.fragment.setting.about_us.AboutUsFragment
import com.songcha.module_mine.ui.fragment.setting.bind_phone.SettingBindPhoneFragment
import com.songcha.module_mine.ui.fragment.setting.personal_recommend.PersonalRecommendFragment
import com.songcha.module_mine.ui.fragment.setting.teenager.TeenagerFragment
import com.songcha.module_mine.ui.fragment.setting.unregister.UnregisterAccountFragment


class SettingActivity:BaseMvvmActivity<SettingViewModel,MineActivitySettingBinding>(),HeaderBarView.IOnHeaderBarGoBackListener,OnBackStackChangedListener,View.OnClickListener{

    override fun getLayoutId(): Int {
        return R.layout.mine_activity_setting
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        supportFragmentManager.addOnBackStackChangedListener(this)

        databinding.mineHbvSetting.setOnGoBackListener(this)
        databinding.mineLlAboutUs.setOnClickListener(this)
        databinding.mineLlBindPhone.setOnClickListener(this)
        databinding.mineLlUserProtocol.setOnClickListener(this)
        databinding.mineLlPrivacyPolicy.setOnClickListener (this)
        databinding.mineLlTeenager.setOnClickListener(this)
        databinding.mineLlUnregisterAccount.setOnClickListener(this)
        databinding.mineLlPersonalRecommend.setOnClickListener(this)
        databinding.mineBtnLogout.setOnClickListener (this)

        ViewHelper.setVisibility(UserInfoManager.checkLogin(), databinding.mineLlBindPhone,
            databinding.mineLlUnregisterAccount,
            databinding.mineLineBindPhone,
            databinding.mineLineUnregister)
        ViewHelper.setVisibility(FunctionConfig.IS_PERSONAL_RECOMMEND_ENABLED,databinding.mineLlPersonalRecommend,databinding.mineLinePersonalRecommend)
    }

    private fun replaceFrag(frag:Fragment,tag:String,title: String){
        hideSettingView()
        val trans=supportFragmentManager.beginTransaction()
        trans.setCustomAnimations(android.R.anim.slide_in_left,
            android.R.anim.slide_out_right,
            android.R.anim.slide_in_left,
            android.R.anim.slide_out_right)
        trans.replace(R.id.mine_frag,frag)
            .addToBackStack(tag)
            .commit()
        setTitle(title)
    }

    override fun onResume() {
        super.onResume()
        if(UserInfoManager.checkLogin()){
            databinding.mineBtnLogout.visibility=View.VISIBLE
        }else{
            databinding.mineBtnLogout.visibility=View.GONE
        }
    }


    private fun hideSettingView(){
        databinding.mineCclSetting.visibility=View.GONE
        databinding.mineBtnLogout.visibility=View.GONE
        //binding.mineFrag.visibility=View.VISIBLE
    }

    private fun showSettingView(){
        databinding.mineCclSetting.visibility=View.VISIBLE
        if(UserInfoManager.checkLogin()){
            databinding.mineBtnLogout.visibility=View.VISIBLE
        }
        //binding.mineFrag.visibility=View.GONE
    }

    private fun setTitle(title:String){
        databinding.mineHbvSetting.setTitle(title)
    }

    override fun onGoBack() {
        if(databinding.mineCclSetting.visibility==View.GONE){

           /* val trans=supportFragmentManager.beginTransaction()
            trans.setCustomAnimations(android.R.anim.slide_in_left,android.R.anim.slide_out_right)
            trans.remove(mCurrentFrag)
            trans.commit()*/

            supportFragmentManager.popBackStack()

            //showSettingView()
            //finish()
            //setTitle("系统设置")
        }else{
            finish()
        }
    }

    override fun onBackStackChanged() {
      /*  if(binding.mineCclSetting.visibility==View.GONE){
            showSettingView()
            setTitle("系统设置")
        }*/
       // LogUtil.log("aaa","onBackStackChanged",supportFragmentManager.fragments.size)

        //show setting view when click back key
        if(supportFragmentManager.fragments.size==0){
            showSettingView()
            setTitle("系统设置")
        }

    }

    override fun onClick(v: View) {
        when(v.id){
            R.id.mine_ll_bind_phone->{
                val frag=SettingBindPhoneFragment()
                replaceFrag(frag,frag.TAG,"")
            }
            R.id.mine_ll_teenager->{
                val frag=TeenagerFragment()
                replaceFrag(frag,frag.TAG,"")
            }
            R.id.mine_ll_user_protocol->{
                BusinessJumpUtil.toUserPrivacyActivity(v.context,1)
            }
            R.id.mine_ll_privacy_policy->{
                BusinessJumpUtil.toUserPrivacyActivity(v.context,2)
            }
            R.id.mine_ll_unregister_account->{
                val frag=UnregisterAccountFragment()
                replaceFrag(frag,frag.TAG,"账号注销")
            }
            R.id.mine_ll_about_us->{
                val frag=AboutUsFragment()
                replaceFrag(frag,frag.TAG,"关于我们")
            }
            R.id.mine_btn_logout->{
                val db= CustomDialog.Companion.Builder(this)
                db.setDialogMsg("是否退出登录？")
                    .setOnConfirmListener {
                        viewModel.logout()
                        ToastUtil.show("已退出登录")
                        finish()
                        true
                    }
                    .create()
                    .show()
            }
            R.id.mine_ll_personal_recommend->{
                val frag=PersonalRecommendFragment()
                replaceFrag(frag,frag.TAG,"个性化推荐")
            }
        }
    }


}